package com.bean;

import java.sql.SQLException;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import com.domain.CallesDomain;
import com.domain.ClientesDomain;
import com.exception.UsuarioYaExisteException;
import com.vo.Calles;
import com.vo.Esquina;

public class ClienteBean extends AbstractBean {
	
	public ClienteBean(){}
	
	public Collection getProducts(){
		Collection clientes = null;
		try {
			clientes=ClientesDomain.getInstance().findAll();
		} catch (SQLException e) {
			this.addError(e);
		}
		return clientes;
	}
	
	public void add(HttpServletRequest req){
		String nombre=req.getParameter("name").toString();
		String direccion=req.getParameter("doorNumber").toString();
		String telefono=req.getParameter("telephone").toString();
		Integer calle=Integer.parseInt(req.getParameter("street").toString());
		Integer esquina=Integer.parseInt(req.getParameter("street1").toString());
		Integer esquina2=null;
		if(req.getParameter("street2")!=null && !"".equals(req.getParameter("street2").toString())){
			esquina2=Integer.parseInt(req.getParameter("street2").toString());
		}
		
		try {
			Integer esq1=CallesDomain.getInstance().getEsquina(calle, esquina).getIdEsquina();
			Integer esq2=null;
			if(esquina2!=null && !"".equals(esquina2)){
				esq2=CallesDomain.getInstance().getEsquina(calle, esquina2).getIdEsquina();
			}
			ClientesDomain.getInstance().addCliente(nombre, direccion, telefono,esq1 , esq2);
		} catch (Exception e) {
			this.addError(e);
		}
	}
	
	/*public void modify(HttpServletRequest req){
		String nombre=req.getAttribute("name").toString();
		int idTipo=Integer.parseInt(req.getAttribute("type").toString());
		String clave=req.getAttribute("password").toString();
		try {
			UsuariosDomain.getInstance().addUsuario(nombre, clave, idTipo);
		} catch (Exception e) {
			this.addError(e);
		}
	}*/
	
	public void remove(HttpServletRequest req){
		int idCliente=Integer.parseInt(req.getParameter("id").toString());
		try {
			ClientesDomain.getInstance().deleteCliente(idCliente);
		} catch (Exception e) {
			this.addError(e);
		}
	}
	
	public Collection<Calles> getCalles(String calle){
		Collection calles=null;
		try {
			calles= CallesDomain.getInstance().getCalleLike(calle);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			this.addError(e);
		}
		return calles;
	}
	
	public String[] getCallesFromEsquinas(Integer idEsquina1, Integer idEsquina2){
		String [] strCalles=null;
		try {
			strCalles=CallesDomain.getInstance().getCallesFromEsquinas(idEsquina1, idEsquina2);
		} catch (SQLException e) {
			this.addError(e);
			e.printStackTrace();
		}
		return strCalles;
	}
}
